package com.zhongwang.mms.module.tool.listner;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.zhongwang.mms.module.tool.callback.EquipmentCallBack;
import com.zhongwang.mms.module.tool.dao.EquipmentDao;
import com.zhongwang.mms.module.tool.model.EquipmentModel;
import com.zhongwang.mms.module.tool.model.ReportEquipmentModel;
import com.zhongwang.mms.util.ShiroUtils;
import org.apache.commons.lang3.ObjectUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 设备台账信息 导入 listener
 *
 * @Author: China.yw
 * @Date: 2020/04/24 9:38
 */
public class EquipmentListener extends AnalysisEventListener<ReportEquipmentModel> {
    private EquipmentDao equipmentDao;
    private EquipmentCallBack equipmentCallBack;
    private List<ReportEquipmentModel> reportEquipmentModels=new ArrayList<>();
    private String eqmMonth="";
    private String eqmFkType="";

    public EquipmentListener(EquipmentDao reportPlanDao,String eqmFkType,String eqmMonth,
                              EquipmentCallBack equipmentCallBack) {
        this.equipmentDao = reportPlanDao;
        this.equipmentCallBack = equipmentCallBack;
        this.eqmMonth=eqmMonth;
        this.eqmFkType=eqmFkType;
    }
    @Override
    public void invoke(ReportEquipmentModel data, AnalysisContext context) {

        /**
         * 验证 厂内编号、设备名称、数量不可以为空，其他的都可以为空。
         */
        if (data.getEqmFacCode() == null || data.getEqmName() == null
                || data.getEqmUnm() == null ) {
            //不合格的数据往这个里放。
            reportEquipmentModels.add(data);
            return;
        }
        /**
         * 设置人员信息和删除标识
         */
        data.setEqmIsdelete(0);

        data.setEqmMonth(eqmMonth);
        ReportEquipmentModel keyModel=new ReportEquipmentModel();
        //根据厂内编号查询
        keyModel.setEqmFacCode(data.getEqmFacCode());
        keyModel.setEqmFkType(eqmFkType);
        keyModel.setEqmMonth(eqmMonth);
        ReportEquipmentModel checkModel=equipmentDao.selectAccEquipment(keyModel);
        if(checkModel!= null){
            //更新参数的model并且赋值
            data.setEqmId(checkModel.getEqmId());
            //前台选择设备台账的类型
            data.setEqmFkType(eqmFkType);
            //执行修改，不存在序号的问题。
            data.setEqmUpdUser(ShiroUtils.getUser().getName());
            equipmentDao.update(data);
        }else {
            data.setEqmFkType(eqmFkType);
            data.setEqmMonth(eqmMonth);
            //需要查询最大的序号，间隔为10。
            data.setEqmSortNum(equipmentDao.selectMaxNum());
            data.setEqmCreUser(ShiroUtils.getUser().getName());
            data.setEqmUpdUser(ShiroUtils.getUser().getName());
            equipmentDao.insert(data);
        }
    }
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        if (reportEquipmentModels.size() > 0) {
            equipmentCallBack.onWarning(reportEquipmentModels);
        } else {
            equipmentCallBack.onSuccess();
        }
    }

    @Override
    public void onException(Exception exception, AnalysisContext context) {
        equipmentCallBack.onError();
    }

}
